const { ElectronEgg } = require('ee-core');
new ElectronEgg();
global.windowMap = {
    data: [],
    windows: new Map(),

    add: function(name, window, createTime,type) {
        const data = {
            name: name,
            windowId: window.id,  // 只存储 window 的 ID
            createTime: createTime,
            type: type
        };
        this.windows.set(window.id, window);
        this.data.push(data);
    },

    getList: function(type) {
        console.log(this.data)
        return [...this.data].filter(item=>item.type === type);
    },

    getWindow: function(id) {
        return this.windows.get(id);
    },

    // 根据 ID 删除列表里的项
    removeById: function(id) {
        // 从 data 列表中删除项
        this.data = this.data.filter(item => item.windowId !== id);
        // 从 windows 中删除项
        this.windows.delete(id);
    },

    // 根据 ID 和名称修改 name
    updateName: function(id, newName) {
        const item = this.data.find(item => item.windowId === id);
        if (item) {
            item.name = newName;
        }
    },

    // 销毁 window 对象
    destroyWindow: function(id) {
        const window = this.windows.get(id);
        if (window) {
            window.close(); // 关闭窗口
            this.windows.delete(id); // 从 windows 中删除项
            // 从 data 列表中删除项
            this.data = this.data.filter(item => item.windowId !== id);
        }
    }
};

